<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;

class FinanceNotifyLog extends Base
{
    protected $table = 'finance_notify_log';
    protected $primaryKey = 'log_id';
    protected $table_user_cloud = 'cloud_user';


    //营业执照注册成功
    const CLOUD_LICENSE_SUCCESS = 4;
    // 税务登记成功
    const CLOUD_LICENSE_TAXATION = 6;

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 创建
     * */
    public function create($data = [])
    {
        $data['created_at'] = time();
        $data['updated_at'] = time();
        $bankId = DB::table($this->table)->insertGetId($data);
        return $bankId;
    }

    /**
     * 修改
     */
    public function edit($data = [], $isSingle = false)
    {
        if ($isSingle) {
            $logId = $data['log_id'];
            unset($data['log_id']);

            $data['updated_at'] = time();

            $edit = FinanceNotifyLog::find($logId);
            foreach ($data as $key => $value) {
                $edit->$key = $value;
            }
            if ($edit->save()) {
                return $logId;
            } else {
                return false;
            }
        } else {
            if (empty($data['where']) or empty($data['field'])) {
                return false;
            }

            $data['field']['updated_at'] = time();

            return DB::table($this->table)->where($data['where'])->update($data['field']);
        }
    }


    /**
     * 查询
     * */
    public function get($data = [], $isSingle = false)
    {
        if ($isSingle) {
            $result = DB::table($this->table)->where($data)->first();
            return self::objToArray($result);
        } else {
            $result = DB::table($this->table)->where($data)->get()->toArray();
            return self::objToArray($result);
        }
    }

    /**获取云账户注册的个体账户信息
     * @param $id_card
     * @return mixed
     */
    public function getCloudUser($id_card){
        $cloud_user = DB::table($this->table_user_cloud)
            ->where('id_card' ,$id_card)
            ->first();
        return $this->objToArray($cloud_user);
    }

    /**获取云账户注册的个体账户信息
     * @param $userId
     * @return mixed
     */
    public function getCloudUserByUserId($userId){
        $cloud_user = DB::table($this->table_user_cloud)
            ->where('user_id' ,$userId)
            ->first();
        return $this->objToArray($cloud_user);
    }

    /**创建/修改云账户个体账户信息
     * @param $params
     * @return int
     */
    public function insertOrUpdateCloudUser($params){
        return DB::INSERT("INSERT INTO zb_{$this->table_user_cloud} (`user_id`,`open_id`,`id_card`,`name`,`status`) VALUE({$params['user_id']}, '{$params['open_id']}','{$params['id_card']}' ,'{$params['name']}' ,{$params['status']}) ON DUPLICATE KEY UPDATE status={$params['status']}");
    }

    /**
     * @param $status
     * @param $userId
     * @desc 根据openId修改本地云账户状态
     */
    public function updateCloudUserStatus($status, $userId)
    {
        DB::table($this->table_user_cloud)
            ->where('user_id' ,$userId)
            ->update(['status' => $status]);

    }

}